import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

/**
 * I love IntelliJ :)
 * Author: shou1dwe
 * Time: 1/5/13 12:56 AM
 */
public class UDPServer {
    private static int PACKET_SIZE = 1024;

    public static void main(String[] args) throws IOException {
        DatagramSocket s = new DatagramSocket(9000);
        byte[] inBuffer = new byte[PACKET_SIZE];
        byte[] outBuffer;
        while (true) {
            DatagramPacket inPacket = new DatagramPacket(inBuffer, inBuffer.length);
            s.receive(inPacket);
            String request = new String(inPacket.getData(), 0, inPacket.getLength());
            System.out.println("request received: "+request);

            String response = request; //just an echo
            outBuffer = response.getBytes();
            DatagramPacket outPacket = new DatagramPacket(outBuffer, outBuffer.length, inPacket.getAddress(), inPacket.getPort());
            s.send(outPacket);
            System.out.println("response sent: "+response);
        }
    }
}
